template <class Alloc> struct allocator_traits;
| member type | definition in unspecialized allocator_traits |
|---|---|
| allocator_type | the template parameter (Alloc) |
| value_type | allocator_type::value_type |
| pointer | allocator_type::pointer (if defined) value_type* (otherwise) |
| const_pointer | allocator_type::const_pointer (if defined) pointer_traits<pointer>::rebind<const value_type> (otherwise) |
| void_pointer | allocator_type::void_pointer (if defined) pointer_traits<pointer>::rebind<void> (otherwise) |
| const_void_pointer | allocator_type::const_void_pointer (if defined) pointer_traits<pointer>::rebind<const void> (otherwise) |
| difference_type | allocator_type::difference_type (if defined) pointer_traits<pointer>::difference_type (otherwise) |
| size_type | allocator_type::size_type (if defined) make_unsigned<difference_type>::type (otherwise) |
| propagate_on_container_copy_assignment | allocator_type::propagate_on_container_copy_assignment (if defined) false_type (otherwise) |
| propagate_on_container_move_assignment | allocator_type::propagate_on_container_move_assignment (if defined) false_type (otherwise) |
| propagate_on_container_swap | allocator_type::propagate_on_container_swap (if defined) false_type (otherwise) |
| rebind_alloc<T> | allocator_type::rebind<T>::other (if defined) AllocTemplate<T,Args> (if Alloc is an instantiation of the form AllocTemplate<U,Args>, where Args are zero or more type arguments) * Note: this is an alias template |
| rebind_traits<T> | allocator_traits<rebind_alloc<T>> * Note: this is an alias template |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// custom allocator example
#include <cstddef>
#include <iostream>
#include <memory>
#include <vector>
template <class T>
struct custom_allocator {
typedef T value_type;
custom_allocator() noexcept {}
template <class U> custom_allocator (const custom_allocator<U>&) noexcept {}
T* allocate (std::size_t n) { return static_cast<T*>(::operator new(n*sizeof(T))); }
void deallocate (T* p, std::size_t n) { ::delete(p); }
};
template <class T, class U>
constexpr bool operator== (const custom_allocator<T>&, const custom_allocator<U>&) noexcept
{return true;}
template <class T, class U>
constexpr bool operator!= (const custom_allocator<T>&, const custom_allocator<U>&) noexcept
{return false;}
int main () {
std::vector<int,custom_allocator<int>> foo = {10,20,30};
for (auto x: foo) std::cout << x << " ";
std::cout << '\n';
return 0;
}
10 20 30